package persistence

import (
	"Data-API-MCP/backend/internal/domain/entity"
	"Data-API-MCP/backend/internal/domain/repository"
	"context"

	"gorm.io/gorm"
)

// APIRepository API仓储实现
type APIRepository struct {
	db *gorm.DB
}

// NewAPIRepository 创建API仓储实例
func NewAPIRepository(db *gorm.DB) repository.APIRepository {
	return &APIRepository{
		db: db,
	}
}

// Create 创建API
func (r *APIRepository) Create(ctx context.Context, api *entity.API) error {
	return r.db.WithContext(ctx).Create(api).Error
}

// Update 更新API
func (r *APIRepository) Update(ctx context.Context, api *entity.API) error {
	return r.db.WithContext(ctx).Save(api).Error
}

// Delete 删除API
func (r *APIRepository) Delete(ctx context.Context, id string) error {
	return r.db.WithContext(ctx).Delete(&entity.API{}, "id = ?", id).Error
}

// FindByID 根据ID查找API
func (r *APIRepository) FindByID(ctx context.Context, id string) (*entity.API, error) {
	var api entity.API
	err := r.db.WithContext(ctx).Where("id = ?", id).First(&api).Error
	if err != nil {
		return nil, err
	}
	return &api, nil
}

// List 获取API列表
func (r *APIRepository) List(ctx context.Context, offset, limit int) ([]*entity.API, error) {
	var apis []*entity.API
	err := r.db.WithContext(ctx).Offset(offset).Limit(limit).Find(&apis).Error
	if err != nil {
		return nil, err
	}
	return apis, nil
}

// Count 获取API总数
func (r *APIRepository) Count(ctx context.Context) (int64, error) {
	var count int64
	err := r.db.WithContext(ctx).Model(&entity.API{}).Count(&count).Error
	return count, err
}

// CreateVersion 创建API版本
func (r *APIRepository) CreateVersion(ctx context.Context, version *entity.APIVersion) error {
	return r.db.WithContext(ctx).Create(version).Error
}

// UpdateVersion 更新API版本
func (r *APIRepository) UpdateVersion(ctx context.Context, version *entity.APIVersion) error {
	return r.db.WithContext(ctx).Save(version).Error
}

// DeleteVersion 删除API版本
func (r *APIRepository) DeleteVersion(ctx context.Context, id string) error {
	return r.db.WithContext(ctx).Delete(&entity.APIVersion{}, "id = ?", id).Error
}

// FindVersionByID 根据ID查找API版本
func (r *APIRepository) FindVersionByID(ctx context.Context, id string) (*entity.APIVersion, error) {
	var version entity.APIVersion
	err := r.db.WithContext(ctx).Where("id = ?", id).First(&version).Error
	if err != nil {
		return nil, err
	}
	return &version, nil
}

// ListVersions 获取API版本列表
func (r *APIRepository) ListVersions(ctx context.Context, apiID string) ([]*entity.APIVersion, error) {
	var versions []*entity.APIVersion
	err := r.db.WithContext(ctx).Where("api_id = ?", apiID).Find(&versions).Error
	if err != nil {
		return nil, err
	}
	return versions, nil
}

// CreateDoc 创建API文档
func (r *APIRepository) CreateDoc(ctx context.Context, doc *entity.APIDoc) error {
	return r.db.WithContext(ctx).Create(doc).Error
}

// UpdateDoc 更新API文档
func (r *APIRepository) UpdateDoc(ctx context.Context, doc *entity.APIDoc) error {
	return r.db.WithContext(ctx).Save(doc).Error
}

// FindDocByAPIID 根据API ID查找文档
func (r *APIRepository) FindDocByAPIID(ctx context.Context, apiID string) (*entity.APIDoc, error) {
	var doc entity.APIDoc
	err := r.db.WithContext(ctx).Where("api_id = ?", apiID).First(&doc).Error
	if err != nil {
		return nil, err
	}
	return &doc, nil
}

// Publish 发布API
func (r *APIRepository) Publish(ctx context.Context, id string) error {
	return r.db.WithContext(ctx).Model(&entity.API{}).Where("id = ?", id).Update("status", 1).Error
}

// Unpublish 取消发布API
func (r *APIRepository) Unpublish(ctx context.Context, id string) error {
	return r.db.WithContext(ctx).Model(&entity.API{}).Where("id = ?", id).Update("status", 0).Error
}

// FindByServiceID 根据服务ID查找API列表
func (r *APIRepository) FindByServiceID(ctx context.Context, serviceID string) ([]*entity.API, error) {
	var apis []*entity.API
	err := r.db.WithContext(ctx).Where("service_id = ?", serviceID).Find(&apis).Error
	if err != nil {
		return nil, err
	}
	return apis, nil
}

// CountByConnectionID 获取指定连接的API总数
func (r *APIRepository) CountByConnectionID(ctx context.Context, connectionID string) (int64, error) {
	var count int64
	err := r.db.WithContext(ctx).Model(&entity.API{}).Where("service_id = ?", connectionID).Count(&count).Error
	return count, err
}

// FindByName 根据名称查找API
func (r *APIRepository) FindByName(ctx context.Context, name string) (*entity.API, error) {
	var api entity.API
	err := r.db.WithContext(ctx).Where("name = ?", name).First(&api).Error
	if err != nil {
		return nil, err
	}
	return &api, nil
}

// ListByConnectionID 获取指定连接的API列表
func (r *APIRepository) ListByConnectionID(ctx context.Context, connectionID string, offset, limit int) ([]*entity.API, error) {
	var apis []*entity.API
	err := r.db.WithContext(ctx).Where("service_id = ?", connectionID).Offset(offset).Limit(limit).Find(&apis).Error
	if err != nil {
		return nil, err
	}
	return apis, nil
}
